package com.jh.concurrent.base.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author JH_Wong
 * @projectName newforward
 * @title InterruptExceptionDemo.java
 * @package com.jh.concurrent.base.interrupt
 * @description 被动中断任务
 * @date 2019-05-09 17:16
 */
public class InterruptExceptionDemo {

    public static int i;

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    i++;
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("inner：" + Thread.currentThread().isInterrupted());
        }, "thread");

        thread.start();
        System.out.println("before：" + thread.isInterrupted());
        TimeUnit.SECONDS.sleep(1);
        thread.interrupt();
        System.out.println("after：" + thread.isInterrupted());

    }

}
